package com.shopping_assistant.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Person {
	
	private String username;
	
	private String password;
	
	private List<VisitCounter> shopsVisited;
	
	private List<OfferViewCounter>	viewedOffers;
	
	private List<VisitedShopType> shopTypeVisited;
	
	public Person(String username, String password)
	{
		this.username = username;
		this.password = password;
		this.shopsVisited = new ArrayList<VisitCounter>();
		this.viewedOffers = new ArrayList<OfferViewCounter>();
		this.shopTypeVisited = new ArrayList<VisitedShopType>();
	}

	public String getUsername() {
		return username;
	}
	
	public String getPassword() {
		return password;
	}
	
	public List<VisitCounter> getShopsVisited() {
		return shopsVisited;
	}
	
	public void addShop(VisitCounter shopVisited) {
		shopsVisited.add(shopVisited);
		
		addFavoriteShopType(shopVisited.getShop().getShopType());
		
		VisitedShopType vShopType = null;
		boolean found = false;
		for(int i = 0; i < shopTypeVisited.size() && !found; i++) {
			vShopType = shopTypeVisited.get(i);
			
			if(vShopType.getShopType() == shopVisited.getShop().getShopType()) {
				vShopType.setCount(vShopType.getTypeVisitedCounter() + shopVisited.getTotalVisits());
				found = true;
			}
		}
		
		Collections.sort(shopsVisited, VisitCounter.compareByCount());
		Collections.sort(shopTypeVisited, new VisitedShopType.CompareByCount());
	}
	
	public List<OfferViewCounter> getOffersViewed() {
		return viewedOffers;
	}
	
	public void addOffer(OfferViewCounter offerViewCounter) {
		viewedOffers.add(offerViewCounter);
		Collections.sort(viewedOffers, OfferViewCounter.compareByViews());
	}
	
	public List<VisitedShopType> getVisitedShopTypes() {
		return shopTypeVisited;
	}
	
	public void shopVisited(Shop shop) {
		
		boolean shopFound = false;
		
		for(VisitCounter object : shopsVisited) {
			if(object.getShop().equals(shop)) {
				object.countVisit();
				shopFound = true;
			}
		}
		
		if(!shopFound) {
			shopsVisited.add(new VisitCounter(this, shop));
		}
		
		Collections.sort(shopsVisited, VisitCounter.compareByCount());
		
		addFavoriteShopType(shop.getShopType());
	}
	
	private void addFavoriteShopType(ShopType shopType) {
		boolean shopFound = false;
		
		for(VisitedShopType object : shopTypeVisited) {
			if(object.getShopType().equals(shopType)) {
				object.countVisit();
				shopFound = true;
			}
		}
		
		if(!shopFound) {
			shopTypeVisited.add(new VisitedShopType(this, shopType));
		}
		
		Collections.sort(shopTypeVisited, new VisitedShopType.CompareByCount());
	}
	
	public void offerViewed(Offer offer) {
		
		boolean offerFound = false;
		
		for(OfferViewCounter object : viewedOffers) {
			if(object.getOffer().equals(offer)) {
				object.countView();
				offerFound = true;
			}
		}
		
		if(!offerFound) {
			viewedOffers.add(new OfferViewCounter(this, offer));
		}
		
		Collections.sort(viewedOffers, OfferViewCounter.compareByViews());
	}
	
	@Override
	public String toString() {
		return "User name: " + this.username + ", Password: " + this.password;
	}
}
